package com.leoandroid.tool.toolsbox.dialog;

import android.app.Dialog;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.leoandroid.tool.toolsbox.adapter.DiscernInfoAdapter;
import com.leoandroid.tool.toolsbox.bean.DiscernInfoBean;
import com.leoandroid.tool.toolsbox.bean.WordInfoBean;
import com.leoandroid.tool.toolsbox.largeread.R;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;

public final class RecognizeTipsDialog extends Dialog {

    private RecyclerView recyclerView;
    private String content = "";
    private TextView textView;
    private ImageView imageView;
    private Context context;
    private NestedScrollView nestedScrollView;
    private TextView copyTextView;

    public RecognizeTipsDialog(@NotNull Context context) {
        super(context);
        this.context = context;
        init();
    }

    public RecognizeTipsDialog(@NotNull Context context, int i) {
        super(context, i);
        this.context = context;
        init();
    }

    public RecognizeTipsDialog(@NotNull Context context, boolean z, @Nullable DialogInterface.OnCancelListener onCancelListener) {
        super(context, z, onCancelListener);
        this.context = context;
        init();
    }

    private void init() {
        View inflate = LayoutInflater.from(context).inflate(R.layout.dialog_recognize_tips, (ViewGroup) null);
        setContentView(inflate);
        this.recyclerView = (RecyclerView) inflate.findViewById(R.id.dialog_discern_list);
        this.copyTextView = (TextView) inflate.findViewById(R.id.copy_tv);
        this.nestedScrollView = (NestedScrollView) inflate.findViewById(R.id.words_scroll_view);
        this.textView = (TextView) inflate.findViewById(R.id.words_tv);
        this.imageView = (ImageView) inflate.findViewById(R.id.discern_img);
        copyTextView.setOnClickListener(view -> {
            ClipboardManager systemService = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
            systemService.setText(content);
            Toast.makeText(context, "复制成功", Toast.LENGTH_SHORT).show();
        });
    }


    public void setWordResult(WordInfoBean wordInfoBean, String path) {
        recyclerView.setVisibility(View.GONE);
        nestedScrollView.setVisibility(View.VISIBLE);
        copyTextView.setVisibility(View.VISIBLE);
        StringBuilder sb = new StringBuilder();
        try {
            for (WordInfoBean.WordsResultBean wordsResultBean : wordInfoBean.getWords_result()) {
                sb.append(wordsResultBean.getWords()).append("\n");
            }
            content = sb.toString();
            textView.setText(content);
            Glide.with(context)
                    .load(path)
                    .apply(RequestOptions.bitmapTransform(new RoundedCorners(50)))
                    .into(imageView);
        } catch (Exception unused) {
        }
    }

    public void setDiscernResult(ArrayList<DiscernInfoBean> arrayList, String path) {
        recyclerView.setVisibility(View.VISIBLE);
        nestedScrollView.setVisibility(View.GONE);
        copyTextView.setVisibility(View.GONE);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(new DiscernInfoAdapter(getContext(), arrayList));
        Glide.with(context)
                .load(path)
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(50)))
                .into(imageView);
    }
}
